import collections
class Solution:
    def maxEqualFreq(self, nums) -> int:
        d_count = collections.defaultdict(int)
        d = collections.defaultdict(int)
        max_ans = 1
        for index, num in enumerate(nums):
            d[num] += 1
            if d[num] == 1:
                d_count[d[num]] += 1
            else:
                d_count[d[num]] += 1
                d_count[d[num] - 1] -= 1
                if d_count[d[num] - 1] == 0:
                    d_count.pop(d[num] - 1)
            if len(d_count) == 2:
                keys = list(d_count.keys())
                keys.sort()
                if keys[0] + 1 == keys[1] and d_count[keys[1]] == 1:
                    max_ans = max(max_ans, index + 1)
                if keys[0] == 1 and d_count[keys[0]] == 1:
                    max_ans = max(max_ans, index + 1)
            if len(d_count) == 1:
                keys = list(d_count.keys())
                if d_count[keys[0]] == 1:
                    max_ans = max(max_ans, index + 1)
                if keys[0] == 1:
                    max_ans = max(max_ans, index + 1)
        return max_ans

data = Solution()
# nums =  [10,2,8,9,3,8,1,5,2,3,7,6]     
# print(data.maxEqualFreq(nums))
nums = [1,2]
print(data.maxEqualFreq(nums))